package com.sina.shop.dao.collect;

import com.sina.shop.dao.ShopDataSourceDao;
import com.sina.shop.domain.collect.CollectBusiness;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-05-14:42
 */
@Repository("collectBusinessDao")
public class CollectBusinessDaoImpl extends ShopDataSourceDao implements CollectBusinessDao {

    @Override
    public List<CollectBusiness> getCollectBusinessByAdminId(Long adminId) {
        String hql = "from CollectBusiness c where c.admin.id= :adminId";
        return list(hql, new String[]{"adminId"}, new Object[]{adminId}, -1, -1);
    }

    @Override
    public Integer deleteCollectBusiness(Long id) {
        String hql = "delete from CollectBusiness c where c.id = :id";
        return template.executeWithNativeSession(session -> {
            Query query = session.createQuery(hql);
            query.setParameter("id", id);

            return query.executeUpdate();
        });
    }

    @Override
    public CollectBusiness getCollectBusinessByAidAndBid(Long adminId, Long businessId) {
        String hql = "from CollectBusiness c where c.admin.id= :adminId and c.business.id= :businessId";
        return unique(hql, new String[]{"adminId", "businessId"}, new Object[]{adminId, businessId});
    }
}
